package com.zxl.admin.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zxl.common.mapper.SysApiMapper;
import com.zxl.common.mapper.SysRoleApiMapper;
import com.zxl.common.mapper.SysRoleMapper;
import com.zxl.common.model.bo.role.RoleAdd;
import com.zxl.common.model.pojo.SysApi;
import com.zxl.common.model.pojo.SysRole;
import com.zxl.common.model.pojo.SysRoleApi;
import com.zxl.common.model.query.role.RoleQuery;
import com.zxl.common.model.vo.role.RoleApiBean;
import com.zxl.common.model.vo.role.RoleApiGroupBean;
import com.zxl.common.service.SysRoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author zxw
 * @date 2023/4/6
 */
@Service
public class RoleService {
    @Resource
    private SysRoleMapper sysRoleMapper;
    @Resource
    private SysRoleApiMapper sysRoleApiMapper;
    @Resource
    private SysApiMapper sysApiMapper;

    @Transactional(rollbackFor = Exception.class)
    public void addRole(String name, String apiStr) {
        SysRole sysRole = new SysRole();
        sysRole.setName(name);
        sysRoleMapper.insert(sysRole);
        String[] split = apiStr.split(",");
        List<String> apis = new ArrayList<>(Arrays.asList(split));
        for (String api : apis) {
            SysRoleApi sysRoleApi = new SysRoleApi();
            sysRoleApi.setApiUrl(api);
            sysRoleApi.setRoleId(sysRole.getId());
            sysRoleApiMapper.insert(sysRoleApi);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void modRole(RoleAdd roleAdd) {
        SysRole sysRole = new SysRole();
        sysRole.setId(roleAdd.getId());
        sysRole.setName(roleAdd.getName());
        sysRoleMapper.updateById(sysRole);

        String apiStr = roleAdd.getApiStr();
        String[] split = apiStr.split(",");
        List<String> apis = new ArrayList<>(Arrays.asList(split));
        if(apis.size()>0){
            sysRoleApiMapper.delete(new QueryWrapper<SysRoleApi>()
                    .eq("role_id",roleAdd.getId())
                    .notIn("api_url",apis));
        }

        List<SysRoleApi> exitRoleApi = sysRoleApiMapper.selectList(new QueryWrapper<SysRoleApi>()
                .eq("role_id", roleAdd.getId()).select("api_url","id"));
        Map<String, SysRoleApi> roleApiMap = new HashMap();
        for (SysRoleApi roleApi : exitRoleApi) {
            roleApiMap.put(roleApi.getApiUrl(), roleApi);

        }

        for(String api:apis){
            SysRoleApi roleApiEntity = roleApiMap.get(api);
            if(roleApiEntity == null){
                SysRoleApi sysRoleApi = new SysRoleApi();
                sysRoleApi.setApiUrl(api);
                sysRoleApi.setRoleId(roleAdd.getId());
                sysRoleApiMapper.insert(sysRoleApi);
            }
        }
    }


    public List<SysRole> list(RoleQuery roleQuery) {
        PageHelper.startPage(roleQuery.getPageNumber(), roleQuery.getPageSize());
        return sysRoleMapper.selectList();

    }

    public List<RoleApiGroupBean> getRoleApi(String roleId) {
        List<SysRoleApi> checkedApiUrls = sysRoleApiMapper.selectList(new QueryWrapper<SysRoleApi>()
                .eq("role_id", roleId));

        Map<String ,String> checkedApiUrlMap=new HashMap<>();

        for(SysRoleApi roleApiEntity : checkedApiUrls){
            checkedApiUrlMap.put(roleApiEntity.getApiUrl(),roleApiEntity.getApiUrl());
        }



        List<SysApi> sysApiEntities = sysApiMapper.selectList(new QueryWrapper<SysApi>());

        List<RoleApiGroupBean> reList=new ArrayList<>();
        Map<String,RoleApiGroupBean> reMap=new HashMap<>();


        //已选择的
        for(SysApi entity:sysApiEntities){
            RoleApiGroupBean groupBean=reMap.get(entity.getGroupName());
            if(groupBean==null){
                groupBean=new RoleApiGroupBean();
                groupBean.setGroupName(entity.getGroupName());
                groupBean.setRoleApi(new ArrayList<>());
                reList.add(groupBean);
                reMap.put(groupBean.getGroupName(),groupBean);
            }
            RoleApiBean b=new RoleApiBean();
            b.setName(entity.getName());
            b.setApiUrl(entity.getUrl());
            b.setChecked(checkedApiUrlMap.get(entity.getUrl())!=null);
            groupBean.getRoleApi().add(b);
        }

        return reList;
    }
}
